(function () {
    var init, initList, bind, initFiltering, getByName, filter, stop, foreach,
            buildFilterURL;
    
    init = function () {
        initList();
        initFiltering();
    };
    
    initFiltering = function () {
        var submit, row, link, inputs;
        submit = document.getElementById('filterButton');
        row = submit.parentNode.parentNode;
        inputs = row.getElementsByTagName('input');
        link = getByName(inputs, '_link').value;
        inputs = filter(inputs, function (input) {
            return input.name && (input.name != '_link');
        });
        bind(submit, 'click', function (e) {
            stop(e);
            location.href = link + '?' + buildFilterURL(inputs);
        });
        foreach(inputs, function (input) {
            bind(input, 'keydown', function (e) {
                if (e.keyCode == 13) {
                    stop(e);
                }
            });
        });
    };
    
    buildFilterURL = function (inputs) {
        var url;
        url = '';
        foreach(inputs, function (input) {
            if (!input.value ||
                    ((input.type == 'checkbox') && !input.checked)) {
                return;
            }
            url += url ? '&' : '';
            url += 'filter[' + input.name + ']=' +
                    encodeURIComponent(input.value);
        });
        return url;
    };
    
    foreach = function (traversable, callback) {
        var i;
        for (i = traversable.length; i--;) {
            callback(traversable[i]);
        }
    };
    
    stop = function (event) {
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    };
    
    filter = function (traversable, callback) {
        var result, i;
        result = [];
        for (i = traversable.length; i--;) {
            callback(traversable[i]) && result.unshift(traversable[i]);
        }
        return result;
    };
    
    getByName = function (items, name) {
        var i;
        for (i = items.length; i--;) {
            if (items[i].name == name) {
                return items[i];
            }
        }
        throw new Error('Item with name ' + name + ' not found!');
    };
    
    initList = function () {
        var checkbox;
        checkbox = document.getElementById('select-all');
        if (!checkbox) {
            return;
        }
        bind(checkbox, 'click', function () {
            var table, checkboxes, i;
            table = checkbox.parentNode;
            while (table.nodeName.toLowerCase() != 'table') {
                table = table.parentNode;
            }
            checkboxes = table.getElementsByTagName('input');
            for (i = checkboxes.length; i--;) {
                (checkboxes[i].name == 'id[]') &&
                        (checkboxes[i].checked = checkbox.checked);
            }
        });
    };
    
    bind = function (element, event, listener) {
        if (element.addEventListener) {
            element.addEventListener(event, listener, false);
        } else {
            element.attachEvent('on' + event, listener);
        }
    };
    
    if (window.addEventListener) {
        addEventListener('DOMContentLoaded', init, false);
    } else {
        attachEvent('onload', init, false);
    }
}());
